<?php

namespace App\Console\Commands;

use App\Library\Logic\LogisticsLogic;
use App\Library\Logic\SalesRecordLogic;
use App\Library\Logic\PaymentLogic;
use App\Library\Logic\PaymentRelatedLogic;
use App\Library\Logic\ShopOrderLogic;
use App\Library\Logic\ShopProductLogic;
use App\Library\YsdLib;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Yansongda\Pay\Pay;

class ProductOrderNotifyCommand extends Command
{
    /**
     * 单商品购买支付回调.
     *
     * @var string
     */
    protected $signature = 'command:productOrderNotify {serialNumber}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * 单商品购买支付回调
     * @throws Exception
     */
    public function handle(): void
    {
        $serialNumber = $this->argument('serialNumber');
        if (empty($serialNumber) || strlen($serialNumber) !== 20) return;
        $order = ShopOrderLogic::findBySerialNumber($serialNumber);
        if (is_null($order) || $order->order_status !== 1) return;
        else if ($order->order_pay_api_status !== 'SUCCESS') return;
        else if ($order->order_pay_type !== 1) return;
        else if ($order->ext->status_pickup === 1) {
            $order->ext->status = 3;
            $order->ext->deliver_at = now();
            $order->ext->status_deliver = 1;
            $order->order_delivery_time = now()->timestamp;
            $order->order_buy_status = 4;
            $order->order_sell_status = 4;
            LogisticsLogic::insertForModel(['objectType' => 1, 'objectId' => $order->order_id]);
        } else $order->ext->status = 2;
        $order->ext->status_pay = 1;
        $order->ext->pay_at = now()->timestamp($order->order_pay_time);
        $order->save();
        $order->ext->save();
        ShopProductLogic::saveSale($order->order_id);
        $where = ['objectType' => $order->getTable(), 'objectId' => $order->order_id];
        if (PaymentRelatedLogic::existsByWhere($where)) return;
        $paymentRelated = PaymentRelatedLogic::insertForModel(array_merge($where, [
            'orderSn' => $order->order_sn,
            'paymentMethodId' => 2,
            'statusPay' => 1,
        ]));
        $result = json_decode($order->order_pay_callback_json, true);
        Pay::config(YsdLib::config());
        PaymentLogic::insertForModel([
            'objectType' => $paymentRelated->object_type,
            'objectId' => $paymentRelated->object_id,
            'paymentRelatedId' => $paymentRelated->id,
            'amount' => prod() ? $order->pay_price : 0.01,
            'paymentMethodId' => $paymentRelated->payment_method_id,
            'initiatorUserId' => $order->buy_user_id,
            'receiveUserId' => 0,
            'payAt' => $order->ext->pay_at,
            'initiatorUserAccountId' => Arr::get($result, 'resource.ciphertext.payer.openid'),
            'receiveUserAccountId' => Arr::get($result, 'resource.ciphertext.mchid'),
            'outTradeNo' => Arr::get($result, 'resource.ciphertext.transaction_id'),
            'result' => $result,
            'find' => Pay::wechat()->query(['out_trade_no' => $order->order_sn]),
        ]);
        SalesRecordLogic::shopOrderInvite($order);
        SalesRecordLogic::addOrEditOfShopOrder($order);
        if ((int)$order->order_is_self_pickup === 1) mq('shopOrderConfirm', $order->serial_number);
        mq('inviteSettlement', $order->serial_number);
        wechatTemplate('newOrder', $order->serial_number);
    }
}
